package com.wang.experiment.experiment3.horseracing;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wr
 * @date 2021/10/27 19:09
 */
public class Rank {
    private Integer rank;
    private final Lock lock = new ReentrantLock();

    public Rank() {
        this.rank = 1;
    }

    public Integer getRank() {
        Integer temp = null;
        //防止并发访问修改rank
        lock.lock();
        try {
            temp = this.rank;
            this.rank++;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return temp;
    }
}
